package huizhi.clone;

public class Person implements Cloneable {
    private String name ;
    private  int age;
    private  int[] score;

    public int[] getScore() {
        return score;
    }

    public Person(String name, int age, int[] score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public void setScore(int[] score) {
        this.score = score;
    }

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public Person clone() {
        try {
            //浅克隆
            // 调用Object类的clone方法得到对象的浅克隆
//            Person clonedPerson = (Person) super.clone();

            //深克隆
            Person clonedPerson = (Person) super.clone();
            clonedPerson.score = this.score.clone();
            // 如果有引用类型的成员变量，需要进行深克隆
            // 在这个例子中，name是String类型，属于不可变对象，可以直接使用

            return clonedPerson;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }
}
